import 'package:flutter/material.dart';

// 定义常量数据
const List<Map<String, dynamic>> kData = [
  {"id": 1, "title": "测试数据AAAA", "subTitle": "子标题AAAA"},
  {"id": 2, "title": "测试数据BBBB", "subTitle": "子标题BBBB"},
  {"id": 3, "title": "测试数据CCCC", "subTitle": "子标题CCCC"},
];

// 定义标题样式常量
const TextStyle kTitleStyle = TextStyle(fontSize: 18.0);

// 封装创建 ListTile 的函数
ListTile createListTile(Map<String, dynamic> item, BuildContext context) {
  return ListTile(
    title: Text(item["title"]?.toString() ?? '', style: kTitleStyle),
    subtitle: Text(item["subTitle"]?.toString() ?? ''),
    leading: const Icon(Icons.fastfood, color: Colors.orange),
    trailing: const Icon(Icons.keyboard_arrow_right),
    onTap: () {
      Navigator.pushNamed(context, "detail_page", arguments: item);
    },
  );
}

class ListPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // 使用 map 方法创建 ListTile 列表
    final List<Widget> list =
        kData.map((item) => createListTile(item, context)).toList();

    return Scaffold(
      appBar: AppBar(title: Text("列表页面")),
      body: ListView(children: list),
    );
  }
}

class DetailPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // 获取路由传参
    final Map<String, dynamic>? args =
        ModalRoute.of(context)?.settings.arguments as Map<String, dynamic>?;

    return Scaffold(
      appBar: AppBar(title: const Text("Detail Page")),
      body: Column(
        children: <Widget>[
          const Text("我是Detail页面"),
          Text("id:${args?['id'] ?? ''}"),
          Text("title:${args?['title'] ?? ''}"),
          Text("subtitle:${args?['subTitle'] ?? ''}"),
        ],
      ),
    );
  }
}
